package com.lutzyt.delivery.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lutzyt.delivery.bean.RolePermission;
import com.lutzyt.delivery.bean.UserRole;
import com.lutzyt.delivery.mapper.RolePermissionDAO;
import com.lutzyt.delivery.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zyt
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionDAO, RolePermission> implements RolePermissionService {

  private RolePermissionDAO rolePermissionDAO;
  public static final String ROLE_ID = "role_id";
  public static final String PERMISSION_ID = "permission_id";

  @Transactional(readOnly = true)
  @Override
  public RolePermission findById(Integer id) {
    final List<RolePermission> list = this.query().eq(ROLE_ID, id).list();
    final List<Integer> permissionIds = list.stream().map(RolePermission::getPermissionId).collect(Collectors.toList());
    RolePermission rolePermission = new RolePermission();
    rolePermission.setPermissionIds(permissionIds);
    return rolePermission;
  }

  @Override
  public List<Integer> findPermissionIdByIds(List<Integer> roleIds) {
    final List<RolePermission> list = this.query().in(ROLE_ID, roleIds).list();
    final List<Integer> permissionIds = list.stream().map(RolePermission::getPermissionId).collect(Collectors.toList());
    return permissionIds;
  }

  @Transactional(readOnly = true)
  @Override
  public IPage<RolePermission> findByPage(int pageNum, int pageSize) {
    return rolePermissionDAO.selectPage(new Page<>(pageNum, pageSize), null);
  }

  @Override
  public void insert(RolePermission rolePermission) {
    final List<Integer> permissionList = rolePermission.getPermissionIds();
    // 删除不存在于roleIds中用户角色映射
    this.update().eq(ROLE_ID, rolePermission.getRoleId())
        .notIn(!permissionList.isEmpty(), PERMISSION_ID, permissionList).remove();
    final List<Integer> permissionIds = this.findById(rolePermission.getRoleId()).getPermissionIds();
    List<RolePermission> rolePermissions = new ArrayList<>();
    permissionList.stream().filter(permissionId->!permissionIds.contains(permissionId)).forEach(permissionId->{
      RolePermission bean = new RolePermission();
      bean.setPermissionId(permissionId);
      bean.setRoleId(rolePermission.getRoleId());
      rolePermissions.add(bean);
    });
    this.saveBatch(rolePermissions);
  }

  @Override
  public void update(RolePermission rolePermission) {
    rolePermissionDAO.updateById(rolePermission);
  }

  @Override
  public void deleteById(Integer id) {
    rolePermissionDAO.deleteById(id);
  }

  @Autowired
  public void setRolePermissionDAO(RolePermissionDAO rolePermissionDAO) {
    this.rolePermissionDAO = rolePermissionDAO;
  }
}